﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using IApp = app_plugin_api;        //主程序接口命名空间
using dbhelp = app_plugin_dbhelper;     //数据库底层封装

namespace app_plugin_main
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 新闻数据结构集合
        /// </summary>
        private List<IApp.News> news = new List<app_plugin_api.News>();

        /// <summary>
        /// 插件集合
        /// </summary>
        private List<IApp.IPluginAPI> plugs = new List<app_plugin_api.IPluginAPI>();        

        /// <summary>
        /// 添加操作日志
        /// </summary>
        /// <param name="log">日志消息</param>
        private void AddLog(string log)
        {
            string msg = string.Format("\r\n{0} {1}\r\n", DateTime.Now.ToString(), log);
            rtxState.Text += msg;

            if (rtxState.Text.Length > 2)
            {
                rtxState.Select(rtxState.Text.Length - 1, rtxState.Text.Length);
                rtxState.ScrollToCaret();
            }
            else if (rtxState.Text.Length > 30000)
            {
                rtxState.Clear();
            }
        }

        /// <summary>
        /// 循环发送新闻
        /// </summary>
        private void WhileSend()
        {
            for (int i = 0; i < news.Count; i++)
            {
                for (int j = 0; j < plugs.Count; j++)
                {
                    string src = plugs[j].SendNews(news[i]); //发送新闻
                    AddLog(src);
                }
            }
        }

        /// <summary>
        /// 加载新闻
        /// </summary>
        private void LoadNews()
        {
            //0=编号 1=标题 2=内容
            string select = "";
            DataTable dt = new DataTable();

            using(dbhelp.DBHelper db = new dbhelp.DBHelper())
            {
                dt = db.GetDataTable(select);
            }
            if (dt != null)
            {
                dgvNews.DataSource = dt;
                news.Clear();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    //0=编号 1=标题 2=内容
                    IApp.News n = new IApp.News();
                    n.Title = dt.Rows[i][1].ToString();
                    n.Content = dt.Rows[i][2].ToString();

                    //添加一条新闻
                    news.Add(n);
                }
            }
        }

        //加载插件
        private void frmMain_Load(object sender, EventArgs e)
        {
            LoadNews();
            AddLog(string.Format("新闻 {0}", news.Count.ToString()));

            //加载插件
            PluginManage _pluginManage = new PluginManage();
            plugs = _pluginManage.plugs;
            AddLog(string.Format("插件 {0}", plugs.Count.ToString()));

            if (news.Count == 0 || plugs.Count == 0)
            {
                //btnStart.Enabled = false;
                btnStop.Enabled = false;
                AddLog("请确保 插件 和 新闻 大于0。");
            }
            else
            {
                btnStart.Enabled = true;
                btnStop.Enabled = true;
            }
        }

        //开始发送
        private void btnStart_Click(object sender, EventArgs e)
        {
            //创建条新闻
            IApp.News n = new app_plugin_api.News();
            n.Title = "问:今天星期几？";
            n.Content = "答:周日。";

            //添加到集合
            news.Add(n);

            //发送出去
            WhileSend();
        }
        
        //添加文章
        private void btnAdd_Click(object sender, EventArgs e)
        {

        }

        //删除文章
        private void btnDel_Click(object sender, EventArgs e)
        {

        }

        //暂停发送
        private void btnStop_Click(object sender, EventArgs e)
        {

        }
    }
}